package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.ShopcarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 17/08/23.
 */
@Controller
@RequestMapping(ControllerConstants.SHOPCAR)
public class ShopCarMCtronller extends BaseMobileController {
    @Autowired
    private ShopcarService shopcarService;

    /**
     * 添加购物车
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addShopCar")
    public void addShopCar(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                Integer bussId = getInteger(json, "bussId", 0);
                Integer isBespeak = getInteger(json, "isBespeak", 0);
                Integer isNoReasonReturn = getInteger(json, "isNoReasonReturn", 0);
                Integer isHomeInstallation = getInteger(json, "isHomeInstallation", 0);
                Integer isFree = getInteger(json, "isFree", 0);
                String unit = getString(json, "unit", "");
                Integer goodsId = getInteger(json, "goodsId", 0);
                Integer number = getInteger(json, "number", 0);
                Integer operator = getInt(json, "operator", 1);
                String goodsSpec = getString(json, "goodsSpec", "[]");
                Double goodsPrice = getDouble(json, "goodsPrice", 0D);
                return shopcarService.addShopCar(userId, bussId, goodsId, number, goodsSpec, operator, isBespeak, isNoReasonReturn, isHomeInstallation, isFree, unit,goodsPrice);
            }
        });
    }

    /**
     * 修改购物车
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateShopCar")
    public void updateShopCar(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                Integer carId = getInteger(json, "carId", 0);
                Integer bussId = getInteger(json, "bussId", 0);
                Integer goodsId = getInteger(json, "goodsId", 0);
                Integer number = getInteger(json, "number", 0);
                String goodsSpec = getString(json, "goodsSpec", "[]");
                return shopcarService.updateShopCar(carId, userId, bussId, goodsId, number, goodsSpec);
            }
        });
    }

    /**
     * 删除购物车
     *
     * @param request
     * @param response
     */
    @RequestMapping("/deleteShopCarById")
    public void deleteShopCarById(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                Integer bussId = getInteger(json, "bussId", 0);

                return shopcarService.deleteShopCarById(userId, bussId);
            }
        });
    }

    /**
     * 删除购物车
     *
     * @param request
     * @param response
     */
    @RequestMapping("/removeAllCar")
    public void removeAllCar(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                return shopcarService.removeAllCar(userId);
            }
        });
    }

    /**
     * 查询购物车
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findShopCar")
    public void findShopCar(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                Integer bussId = getInteger(json, "bussId", 0);

                return shopcarService.findShopCar(userId, bussId);
            }
        });
    }
}
